﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data.Providers;
using System.Data;
using System.Data.SqlClient;
using DDF.Data;

namespace ARR.DataModel.Repositories
{
    public static partial class TempStoresRepository
    {        
        public static void TruncateTable()
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TRUNCATE TABLE TempStores ", System.Data.CommandType.Text);

                database.ExecuteNonQuery();
            }
        }

        public static void BulkCopy(DataTable storesToCopy)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
                {
                    bulk.DestinationTableName = "TempStores";

                    bulk.ColumnMappings.Add("CountryId", "CountryId");
                    bulk.ColumnMappings.Add("Code", "Code");
                    bulk.ColumnMappings.Add("Name", "Name");
                    bulk.ColumnMappings.Add("Region", "Region");
                    bulk.ColumnMappings.Add("Subregion", "Subregion");
                    bulk.ColumnMappings.Add("City", "City");
                    bulk.ColumnMappings.Add("State", "State");
                    bulk.ColumnMappings.Add("SEL", "SEL");
                    bulk.ColumnMappings.Add("ShoppingOccasion", "ShoppingOccasion");
                    bulk.ColumnMappings.Add("Size", "Size");
                    bulk.ColumnMappings.Add("Address", "Address");
                    bulk.ColumnMappings.Add("Phone", "Phone");
                    bulk.ColumnMappings.Add("Fax", "Fax");
                    bulk.ColumnMappings.Add("Format", "Format");
                    bulk.ColumnMappings.Add("PBL", "PBL");
                    bulk.ColumnMappings.Add("Mail", "Mail");
                    bulk.ColumnMappings.Add("Contact", "Contact");
                    bulk.ColumnMappings.Add("SiteLocation", "SiteLocation");
                    bulk.ColumnMappings.Add("ShopType", "ShopType");
                    bulk.ColumnMappings.Add("Layout", "Layout");
                    bulk.ColumnMappings.Add("OpeningHours", "OpeningHours");
                    bulk.ColumnMappings.Add("Attribute1", "Attribute1");
                    bulk.ColumnMappings.Add("Attribute2", "Attribute2");

                    bulk.ColumnMappings.Add("SELCustomers", "SELCustomers");
                    bulk.ColumnMappings.Add("SELLocation", "SELLocation");
                    
                    bulk.BulkCopyTimeout = 300;
                    bulk.WriteToServer(storesToCopy);
                    bulk.Close();
                }

                conn.Close();
            }
        }

        public static void DeleteDuplicates()
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("TempStores_DeleteDuplicates", CommandType.StoredProcedure);
                database.ExecuteNonQuery();
            }
        }

        public static void InsertDataFromTempSales()
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("TempStores_InsertDataFromTempSales", CommandType.StoredProcedure);
                database.ExecuteNonQuery();
            }
        }
    }
}
